var battleMsgQueue = []

function showBattleMsg(msg, info) {
  if (info) {
    battleMsgQueue.push([msg, info])
  } else {
    battleMsgQueue.push([msg])
  }
}

function attacks(fightMsg, cb) {
  $('#main').append(template('t_battleBg'))
  $('#main').append(template('t_battlePop'))

  var p1 = fightMsg.p1
  var p2 = fightMsg.p2

  var skills = p1.fightAttri.skills
  for (var i in skills) {
    var con = '<span class="icon-' + skillsSvgs[skills[i]] + '"></span>'
    $('.p1 .skills').append(con)
  }
  var skills = p2.fightAttri.skills
  for (var i in skills) {
    var con = '<span class="icon-' + skillsSvgs[skills[i]] + '"></span>'
    $('.p2 .skills').append(con)
  }
  setTimeout(function() {
    var timer = setInterval(function() {
      if (battleMsgQueue.length > 0) {
        var msg = battleMsgQueue.shift()
        if (msg[0] !== '') {
          $('.battleMsg').prepend('<div>' + msg[0] + '</div>')
        }
        if ($.isArray(msg[1])) {
          $('.p1 .progress-bar').css('width', msg[1][0] / msg[1][1] * 100 + '%')
          $('.hp1').html(msg[1][0])
          $('.maxHp1').html(msg[1][1])
          $('.p2 .progress-bar').css('width', msg[1][2] / msg[1][3] * 100 + '%')
          $('.hp2').html(msg[1][2])
          $('.maxHp2').html(msg[1][3])
        } else if ($.isFunction(msg[1])) {
          msg[1]()
          clearInterval(timer)
        }
      }
    }, 500)
  }, 500)

  var seed = fightMsg.seed
  var random = new Random(seed)

  var fightAttri1 = p1.fightAttri
  fightAttri1.name = p1.name
  var fightAttri2 = p2.fightAttri
  fightAttri2.name = p2.name

  $('.p1 .progress-bar').css('width', fightAttri1.hp / fightAttri1.maxHp * 100 + '%')
  $('.hp1').html(fightAttri1.hp)
  $('.maxHp1').html(fightAttri1.maxHp)
  $('.p2 .progress-bar').css('width', fightAttri2.hp / fightAttri2.maxHp * 100 + '%')
  $('.hp2').html(fightAttri2.hp)
  $('.maxHp2').html(fightAttri2.maxHp)
  var damage = 0
  var prefixAttriName1 = '',
    prefixAttriName2 = ''
  if (fightAttri1.prefixAttriId) {
    var config = templateConfig.prefixAttri[fightAttri1.prefixAttriId]
    if (config.lvl === 2) {
      fightAttri1.name = '<span class="itemColor2">' + config.name + '▪</span>' + p1.name
    } else {
      fightAttri1.name = config.name + '▪' + p1.name
    }

  }

  if (fightAttri2.prefixAttriId) {
    var config = templateConfig.prefixAttri[fightAttri2.prefixAttriId]
    if (config.lvl === 2) {
      fightAttri2.name = '<span class="itemColor2">' + config.name + '▪</span>' + p2.name
    } else {
      fightAttri2.name = config.name + '▪' + p2.name
    }
  }

  var skillLen = fightAttri1.attackSkill.length + fightAttri1.defendSkill.length
  if (skillLen > 0) {
    fightAttri1['perSkillRate'] = fightAttri1.skillRate / (fightAttri1.attackSkill.length + fightAttri1.defendSkill.length)
  } else {
    fightAttri1['perSkillRate'] = 0
  }

  skillLen = fightAttri2.attackSkill.length + fightAttri2.defendSkill.length
  if (skillLen > 0) {
    fightAttri2['perSkillRate'] = fightAttri2.skillRate / (fightAttri2.attackSkill.length + fightAttri2.defendSkill.length)
  } else {
    fightAttri2['perSkillRate'] = 0
  }

  $('.p1 .pname').html(fightAttri1.name + ' Lv' + fightAttri1.lvl)
  $('.p2 .pname').html(fightAttri2.name + ' Lv' + fightAttri2.lvl)
  fightAttri1.pos = 1
  fightAttri2.pos = 2
  while (fightAttri1.hp > 0 && fightAttri2.hp > 0) {
    calcAttack(fightAttri1, fightAttri2, random)
    if (fightAttri1.hp > 0 && fightAttri2.hp > 0) {
      calcAttack(fightAttri2, fightAttri1, random)
    }
  }
  console.log(fightAttri1.hp + '________' + fightAttri2.hp)
  if (fightMsg.result === 1) {
    showBattleMsg('*** ' + p1.name + '获胜了')
    if (fightMsg.exp) {
      showBattleMsg('*** 获得 ' + fightMsg.exp + ' 经验')
    }
    if (fightMsg.money) {
      showBattleMsg('*** 获得 ' + fightMsg.money + ' 银币')
    }
    if (fightMsg.equipment) {
      var classtxt = ''
      if (fightMsg.equipment.quality === 2) {
        classtxt = 'itemColor1'
      } else if (fightMsg.equipment.quality === 3) {
        classtxt = 'itemColor2'
      } else if (fightMsg.equipment.quality === 4) {
        classtxt = 'itemColor3'
      }
      showBattleMsg('*** 获得装备 <span class="' + classtxt + '">' + fightMsg.equipment.name + '▪' + propName[fightMsg.equipment.templateId] + '<span>')
    }

    if (fightMsg.dropProp) {
      showBattleMsg('*** 获得物品 ' + propName[fightMsg.dropProp] + '')
    }
  } else {
    showBattleMsg('*** ' + p1.name + ' 阵亡了')
  }
  showBattleMsg('', function() {
    if (!cb) {
      $('#battleBg,#battleBox').click(function() {
        $('#battleBg,#battleBox').remove()
      })
    } else {
      cb()
    }
  })

  return fightMsg

  //计算攻击
  function calcAttack(attacker, defender, random) {
    var damage = 0
    if (attacker.debuff === 1) {
      showBattleMsg(attacker.name + "眩晕中,无法攻击")
      attacker.debuff = 0
      return damage
    }

    // 计算防御者技能触发
    var defendSkillRate = random.getRand()
    if (defendSkillRate < defender.perSkillRate * defender.defendSkill.length) {
      var index = Math.floor(defendSkillRate / defender.perSkillRate)
      var defendSkillId = defender.defendSkill[index]
      defendSkills[defendSkillId](attacker, defender, random)
    } else {
      calcAttackSkill(attacker, defender, random)
    }
  }

}

function calcAttackSkill(attacker, defender, random, defendContent, defendPer) {
  var damage = 0
    //计算攻击者技能触发
  var attackSkillRate = random.getRand()
  if (attackSkillRate < attacker.perSkillRate * attacker.attackSkill.length) {
    var index = Math.floor(attackSkillRate / attacker.perSkillRate)
    var attackSkillId = attacker.attackSkill[index]
    showBattleMsg(attacker.name + '触发' + propName[attackSkillId] + '<span class="icon-' + skillsSvgs[attackSkillId] + '"></span>')
    if (defendContent) {
      showBattleMsg(defendContent)
    }
    var j = 1
    if (attackSkillId === 450001 || attackSkillId === 450002) {
      j = 2
    }
    for (var k = 0; k < j; k++) {
      var battleInfo = attackSkills[attackSkillId](attacker, defender, random)
      damage1 = Math.ceil(battleInfo.damage * (1 + attacker.skillHurtPer / 100))
      if (defendPer) {
        damage1 = Math.ceil(damage1 * defendPer)
      }
      defender.hp -= damage1
      if (defender.hp < 0) {
        damage1 += defender.hp
        defender.hp = 0
      }
      if (damage1 > 0) {
        var txt = ''
        if (battleInfo.critFlag) {
          txt = '<span class="colorCrit">' + damage1 + '暴击</span>伤害'
        } else {
          txt = damage1 + '伤害'
        }
        if (attacker.pos === 1) {
          showBattleMsg(defender.name + "受到" + txt, [attacker.hp, attacker.maxHp, defender.hp, defender.maxHp])
        } else {
          showBattleMsg(defender.name + "受到" + txt, [defender.hp, defender.maxHp, attacker.hp, attacker.maxHp])
        }
      }

      damage += damage1
    }
    if(attackSkillId === 450010){
      var recover = Math.ceil(damage * 0.40)
      attacker.hp += recover
      if (attacker.hp > attacker.maxHp) {
        recover -= attacker.hp - attacker.maxHp
        attacker.hp = attacker.maxHp
      }
      if (attacker.pos === 1) {
        showBattleMsg(attacker.name + '吸取了<span class="recoverHp">' + recover + '</span>点生命', [attacker.hp, attacker.maxHp, defender.hp, defender.maxHp])
      } else {
        showBattleMsg(attacker.name + '吸取了<span class="recoverHp">' + recover + '</span>点生命', [defender.hp, defender.maxHp, attacker.hp, attacker.maxHp])
      }
    }else if(attackSkillId === 450011){
      var recover = Math.ceil(damage * 0.50)
      attacker.hp += recover
      if (attacker.hp > attacker.maxHp) {
        recover -= attacker.hp - attacker.maxHp
        attacker.hp = attacker.maxHp
      }
      if (recover > 0) {
        if (attacker.pos === 1) {
          showBattleMsg(attacker.name + '吸取了<span class="recoverHp">' + recover + '</span>点生命', [attacker.hp, attacker.maxHp, defender.hp, defender.maxHp])
        } else {
          showBattleMsg(attacker.name + '吸取了<span class="recoverHp">' + recover + '</span>点生命', [defender.hp, defender.maxHp, attacker.hp, attacker.maxHp])
        }
      }
    }
  } else {
    var battleInfo = calcDamage(attacker, defender, random)
    if (defendContent) {
      showBattleMsg(defendContent)
    }
    damage = battleInfo.damage
    if (defendPer) {
      damage = Math.ceil(damage * defendPer)
    }
    defender.hp -= damage
    if (defender.hp < 0) {
      damage += defender.hp
      defender.hp = 0
    }
    var txt = ''
    if (battleInfo.critFlag) {
      txt = '<span class="colorCrit">' + damage + '暴击</span>伤害'
    } else {
      txt = damage + '伤害'
    }
    if (attacker.pos === 1) {
      showBattleMsg(defender.name + "受到" + txt, [attacker.hp, attacker.maxHp, defender.hp, defender.maxHp])
    } else {
      showBattleMsg(defender.name + "受到" + txt, [defender.hp, defender.maxHp, attacker.hp, attacker.maxHp])
    }
  }
  return damage
}

//计算伤害
function calcDamage(attacker, defender, random) {
  var defend = defender.defend - attacker.ignoreDefend
  if (defend < 0) {
    defend = 0
  }
  var critFlag = 0
  var damage = attacker.attack * attacker.attack / (defend + attacker.attack) //计算伤害
    //计算暴击
  if (random.getRand() < attacker.critRate) {
    damage *= (1 + attacker.critHurt / 100)
    critFlag = 1
  }
  if (damage <= 0) {
    damage = 1
  }
  return {
    damage : Math.ceil(damage),
    critFlag: critFlag
  }
}

//攻击技能
var attackSkills = {
  //连击: 造成两次攻击, 单次伤害降低40%
  450001: function skill_450001(attacker, defender, random) {
    var battleInfo = calcDamage(attacker, defender, random)
    battleInfo.damage = battleInfo.damage * 0.60
    return battleInfo
  },
  //连击: 造成两次攻击, 单次伤害降低30%
  450002: function skill_450002(attacker, defender, random) {
    var battleInfo = calcDamage(attacker, defender, random)
    battleInfo.damage = battleInfo.damage * 0.70
    return battleInfo
  },
  //嗜血: 造成伤害的时候，将50%的伤害转化为自己的生命。
  450010: function skill_450010(attacker, defender, random) {
    battleInfo = calcDamage(attacker, defender, random)

    return battleInfo
  },
  //嗜血: 造成伤害的时候，将50%的伤害转化为自己的生命。
  450011: function skill_450011(attacker, defender, random) {
    battleInfo = calcDamage(attacker, defender, random)
    var damage = battleInfo.damage

    return battleInfo
  },
  //重击: 造成60%的伤害，并击晕敌人。
  450020: function skill_450020(attacker, defender, random) {
    var battleInfo = calcDamage(attacker, defender, random)
    var damage = battleInfo.damage
    defender.debuff = 1
    battleInfo.damage = battleInfo.damage * 0.3
    return battleInfo
  },
  //进阶重击: 造成45%的伤害，并击晕敌人。
  450021: function skill_450021(attacker, defender, random) {
    var battleInfo = calcDamage(attacker, defender, random)
    var damage = battleInfo.damage
    defender.debuff = 1
    battleInfo.damage = battleInfo.damage * 0.45
    return battleInfo
  },
  //春风: 恢复自身15%血量
  450030: function skill_450030(attacker, defender, random) {
    var recover = Math.ceil(attacker.maxHp * 0.15 * (1 + attacker.skillHurtPer / 100))
    attacker.hp += recover
    if (attacker.hp > attacker.maxHp) {
      recover -= attacker.hp - attacker.maxHp
      attacker.hp = attacker.maxHp
    }
    if (attacker.pos === 1) {
      showBattleMsg(attacker.name + '恢复了<span class="recoverHp">' + recover + '生命</span>', [attacker.hp, attacker.maxHp, defender.hp, defender.maxHp])
    } else {
      showBattleMsg(attacker.name + '恢复了<span class="recoverHp">' + recover + '生命</span>', [defender.hp, defender.maxHp, attacker.hp, attacker.maxHp])
    }

    return {
      damage: 0,
      critFlag: 0
    }
  },
  //进阶春风: 恢复自身20%血量
  450031: function skill_450031(attacker, defender, random) {
    var recover = Math.ceil(attacker.maxHp * 0.2 * (1 + attacker.skillHurtPer / 100))
    attacker.hp += recover
    if (attacker.hp > attacker.maxHp) {
      recover -= attacker.hp - attacker.maxHp
      attacker.hp = attacker.maxHp
    }
    if (attacker.pos === 1) {
      showBattleMsg(attacker.name + '恢复了<span class="recoverHp">' + recover + '生命</span>', [attacker.hp, attacker.maxHp, defender.hp, defender.maxHp])
    } else {
      showBattleMsg(attacker.name + '恢复了<span class="recoverHp">' + recover + '生命</span>', [defender.hp, defender.maxHp, attacker.hp, attacker.maxHp])
    }

    return {
      damage: 0,
      critFlag: 0
    }
  },
  //斩杀:提升30暴击率, 和20%的暴击伤害
  450040: function skill_450040(attacker, defender, random) {
    var defend = defender.defend - attacker.ignoreDefend
    if (defend < 0) {
      defend = 0
    }
    var critFlag = 0
    var damage = attacker.attack * attacker.attack / (defend + attacker.attack) //计算伤害
      //计算暴击
    if (random.getRand() * 100 < (attacker.critRate + 30)) {
      damage *= 1 + (attacker.critHurt + 20) / 100
      critFlag = 1
    }
    if (damage <= 0) {
      damage = 1
    }
    return {
      damage: damage,
      critFlag: critFlag
    }
  },
  //斩杀:提升30暴击率, 和40%的暴击伤害
  450041: function skill_450041(attacker, defender, random) {
    var defend = defender.defend - attacker.ignoreDefend
    if (defend < 0) {
      defend = 0
    }
    var critFlag = 0
    var damage = attacker.attack * attacker.attack / (defend + attacker.attack) //计算伤害
      //计算暴击
    if (random.getRand() * 100 < (attacker.critRate + 30)) {
      damage *= 1 + (attacker.critHurt + 40) / 100
      critFlag = 1
    }
    if (damage <= 0) {
      damage = 1
    }
    return {
      damage: damage,
      critFlag: critFlag
    }
  },

  //凝风: 造成120%的伤害
  450050: function skill_450050(attacker, defender, random) {
    var battleInfo = calcDamage(attacker, defender, random)
    var damage = battleInfo.damage
    battleInfo.damage = battleInfo.damage * 1.2
    return battleInfo
  },
  //山崩: 造成130%的伤害
  450051: function skill_450051(attacker, defender, random) {
    var battleInfo = calcDamage(attacker, defender, random)
    var damage = battleInfo.damage
    battleInfo.damage = battleInfo.damage * 1.3
    return battleInfo
  },
  //烈日: 造成140%的伤害
  450052: function skill_450052(attacker, defender, random) {
    var battleInfo = calcDamage(attacker, defender, random)
    var damage = battleInfo.damage
    battleInfo.damage = battleInfo.damage * 1.4
    return battleInfo
  },

  //雨刃: 造成等级*6的固定伤害
  450060: function skill_450060(attacker, defender, random) {
    var damage = attacker.lvl * 6
    return {
      damage: damage,
      critFlag: 0
    }
  },
  //烈日: 造成等级*7的固定伤害
  450061: function skill_450061(attacker, defender, random) {
    var damage = attacker.lvl * 7
    return {
      damage: damage,
      critFlag: 0
    }
  },
  //雷击: 造成等级*8的固定伤害
  450062: function skill_450062(attacker, defender, random) {
    var damage = attacker.lvl * 8
    return {
      damage: damage,
      critFlag: 0
    }
  },
}

var defendSkills = {
  //灵敏:闪避敌人的攻击
  450101: function skill_450101(attacker, defender, random) {
    showBattleMsg(defender.name + '触发了<span class="icon-' + skillsSvgs[450101] + '"></span>' + '闪避了' + attacker.name + '的攻击')
    return 0
  },
  //防御: 降低 30%的伤害
  450110: function skill_450110(attacker, defender, random) {
    var content = defender.name + '触发了<span class="icon-' + skillsSvgs[450110] + '"></span>' + '进行了防御'
    var damage = calcAttackSkill(attacker, defender, random, content, 0.7)
    return
  },
  //防御: 降低 50%的伤害
  450111: function skill_450111(attacker, defender, random) {
    var content = defender.name + '触发了<span class="icon-' + skillsSvgs[450110] + '"></span>' + '进行了防御'
    var damage = calcAttackSkill(attacker, defender, random, content, 0.5)
    return damage * 0.5
  },
  //反击: 反击对手，反击伤害为攻击伤害的40%。
  450120: function skill_450120(attacker, defender, random) {
    var damage = calcAttackSkill(attacker, defender, random)
    if (damage > 0 && defender.hp > 0) {
      var battleInfo = calcDamage(defender, attacker, random)
      var counterDamage = Math.ceil(battleInfo.damage * 0.4)
      attacker.hp -= counterDamage

      if (attacker.hp < 0) {
        counterDamage += attacker.hp
        attacker.hp = 0
      }
      showBattleMsg(defender.name + '触发了<span class="icon-' + skillsSvgs[450120] + '"></span>' + '反击了' + attacker.name + '的攻击')
      if (attacker.pos === 1) {
        showBattleMsg('造成了' + counterDamage + '伤害', [attacker.hp, attacker.maxHp, defender.hp, defender.maxHp])
      } else {
        showBattleMsg('造成了' + counterDamage + '伤害', [defender.hp, defender.maxHp, attacker.hp, attacker.maxHp])
      }
    }
    return damage
  },
  //反击: 反击对手，反击伤害为攻击伤害的60%。
  450121: function skill_450121(attacker, defender, random) {
    var damage = calcAttackSkill(attacker, defender, random)
    if (damage > 0 && defender.hp > 0) {
      var battleInfo = calcDamage(defender, attacker, random)
      var counterDamage = Math.ceil(battleInfo.damage * 0.6)
      attacker.hp -= counterDamage
      if (attacker.hp < 0) {
        counterDamage += attacker.hp
        attacker.hp = 0
      }
      showBattleMsg(defender.name + '触发了<span class="icon-' + skillsSvgs[450121] + '"></span>' + '反击了' + attacker.name + '的攻击')
      if (attacker.pos === 1) {
        showBattleMsg('造成了' + counterDamage + '伤害', [attacker.hp, attacker.maxHp, defender.hp, defender.maxHp])
      } else {
        showBattleMsg('造成了' + counterDamage + '伤害', [defender.hp, defender.maxHp, attacker.hp, attacker.maxHp])
      }
    }
    return damage
  },
}
